/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.element.force.ElementForce;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmDistForcesDialog dgl = new ElmDistForcesDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmDistForcesDialog extends SimpleDialog {
	
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private NumberFormat numberFormat2;
	private CardLayout cardManager = new CardLayout();
	private FemModel fmodel;
	
	private String anlType;
	
	private JPanel distButPane = new JPanel();
	private boolean distNewPressed = false;
	private String[] distLabelList;
	private JComboBox distListCombo = new JComboBox();
	private JButton distNewButton = new JButton();
	private JButton distModButton = new JButton();
	private JButton distOkButton = new JButton();
	private JButton distCancButton = new JButton();
	private JTextField distLabelField = new JTextField();
	private DecimalField distAField;
	private DecimalField distBField;
	private DecimalField distFx1Field;
	private DecimalField distFy1Field;
	private DecimalField distFz1Field;
	private DecimalField distMx1Field;
	private DecimalField distFx2Field;
	private DecimalField distFy2Field;
	private DecimalField distFz2Field;
	private DecimalField distMx2Field;
	public ArrayList<ElementForce> distForces = new ArrayList<ElementForce>();
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.Interface
	 */
	public ElmDistForcesDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		distNewButton.setText(parent.getResourceBundle().getString("gNewButton"));
		distModButton.setText(parent.getResourceBundle().getString("gModifyButton"));
		distOkButton.setText(parent.getResourceBundle().getString("gOkButton"));
		distCancButton.setText(parent.getResourceBundle().getString("gCancelButton"));
		
		setUpFormat();
		setUpFields();
		
		setUpDistList();
		setUpDistCombo();
		
		//Sets up the distributed forces panel
		distListCombo = new JComboBox(distLabelList);
		
		JPanel distForcesPane = new JPanel();
		distForcesPane.setPreferredSize(new Dimension(360,350));
		distForcesPane.setLayout(new BoxLayout(distForcesPane, BoxLayout.Y_AXIS));
		
		distListCombo.setMaximumSize(new Dimension(100,26));
		distNewButton.setMaximumSize(new Dimension(100,26));
		distModButton.setMaximumSize(new Dimension(100,26));
		distOkButton.setMaximumSize(new Dimension(100,26));
		distCancButton.setMaximumSize(new Dimension(100,26));
		
		distListCombo.setMaximumRowCount(5);
		
		distListCombo.addItemListener(new DistComboListener());
		distNewButton.addActionListener(new DistNewListener());
		distModButton.addActionListener(new DistModListener());
		distOkButton.addActionListener(new DistOkListener());
		distCancButton.addActionListener(new DistCancListener());
		
		distLabelField.setEditable(false);
		distAField.setEditable(false);
		distBField.setEditable(false);
		distFx1Field.setEditable(false);
		distFy1Field.setEditable(false);
		distFz1Field.setEditable(false);
		distMx1Field.setEditable(false);
		distFx2Field.setEditable(false);
		distFy2Field.setEditable(false);
		distFz2Field.setEditable(false);
		distMx2Field.setEditable(false);
		
		String imgName = "br/ufmg/dees/insane/images/" + parent.getResourceBundle().getString("imgDistForces100");
		
		JPanel distPicPanel = new JPanel();
		distPicPanel.setMaximumSize(new Dimension(500,100));
		distPicPanel.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource(imgName)) ));
		distForcesPane.add(distPicPanel);
		
		distButPane.setLayout(cardManager);
		
		JPanel distBut1Pane = new JPanel();
		distBut1Pane.setLayout(new GridLayout(1,2,2,2));
        distBut1Pane.setMaximumSize(new Dimension(500,120));
		JPanel distBut1aPane = new JPanel();
		distBut1aPane.setLayout(new GridLayout(1,2,2,2));
		distBut1aPane.add(distNewButton);
		distBut1aPane.add(distModButton);
		distBut1Pane.add(distListCombo);
		distBut1Pane.add(distBut1aPane);
		
		JPanel distBut2Pane = new JPanel();
		distBut2Pane.setLayout(new GridLayout(1,2,2,2));
        distBut2Pane.setMaximumSize(new Dimension(500,120));
		distBut2Pane.add(distLabelField);
		JPanel distBut2aPane = new JPanel();
		distBut2aPane.setLayout(new GridLayout(1,2,2,2));
		distBut2aPane.add(distOkButton);
		distBut2aPane.add(distCancButton);
		distBut2Pane.add(distBut2aPane);
		
		distButPane.add(distBut1Pane, "distCombo");
		distButPane.add(distBut2Pane, "distLabel");
		
		distForcesPane.add(distButPane);
		
		JPanel distDistPane = new JPanel();
		distDistPane.setLayout(new GridLayout(1,2,2,2));
        distDistPane.setMaximumSize(new Dimension(500,80));
		distDistPane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmDistForDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		JPanel distAPane = new JPanel();
		distAPane.add(new JLabel("A:"));
		distAPane.add(distAField);
		distDistPane.add(distAPane);
		JPanel distBPane = new JPanel();
		distBPane.add(new JLabel("B:"));
		distBPane.add(distBField);
		distDistPane.add(distBPane);
		distForcesPane.add(distDistPane);
		
		JPanel distValuesPanel = new JPanel();
		distValuesPanel.setLayout(new GridLayout(1,2,0,0));
		
		JPanel distValues1Pane = new JPanel();
		distValues1Pane.setLayout(new GridLayout(4,1,2,2));
        distValues1Pane.setMaximumSize(new Dimension(500,300));
		distValues1Pane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmDistForDialog02")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel distFx1Panel = new JPanel();
		distFx1Panel.add(new JLabel("Fx:"));
		distFx1Panel.add(distFx1Field);
		distValues1Pane.add(distFx1Panel);
		JPanel distFy1Panel = new JPanel();
		distFy1Panel.add(new JLabel("Fy:"));
		distFy1Panel.add(distFy1Field);
		distValues1Pane.add(distFy1Panel);
		JPanel distFz1Panel = new JPanel();
		distFz1Panel.add(new JLabel("Fz:"));
		distFz1Panel.add(distFz1Field);
		distValues1Pane.add(distFz1Panel);
		JPanel distMx1Panel = new JPanel();
		distMx1Panel.add(new JLabel("Mx:"));
		distMx1Panel.add(distMx1Field);
		distValues1Pane.add(distMx1Panel);
		
		distValuesPanel.add(distValues1Pane);
		
		JPanel distValues2Pane = new JPanel();
		distValues2Pane.setLayout(new GridLayout(4,1,2,2));
        distValues2Pane.setMaximumSize(new Dimension(500,300));
		distValues2Pane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmDistForDialog03")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel distFx2Panel = new JPanel();
		distFx2Panel.add(new JLabel("Fx:"));
		distFx2Panel.add(distFx2Field);
		distValues2Pane.add(distFx2Panel);
		JPanel distFy2Panel = new JPanel();
		distFy2Panel.add(new JLabel("Fy:"));
		distFy2Panel.add(distFy2Field);
		distValues2Pane.add(distFy2Panel);
		JPanel distFz2Panel = new JPanel();
		distFz2Panel.add(new JLabel("Fz:"));
		distFz2Panel.add(distFz2Field);
		distValues2Pane.add(distFz2Panel);
		JPanel distMx2Panel = new JPanel();
		distMx2Panel.add(new JLabel("Mx:"));
		distMx2Panel.add(distMx2Field);
		distValues2Pane.add(distMx2Panel);
		
		distValuesPanel.add(distValues2Pane);
		distForcesPane.add(distValuesPanel);
		
		setPane(distForcesPane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
        return super.showDialog(parent, parent.getResourceBundle().getString("dElmDistForDialog04"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
		
		numberFormat2 = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat2.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat2.setMaximumFractionDigits(parent.getModelState().getPrecision());
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			//Setting distributed forces fields
			//Copying actual ElmLineForcesList
			ListIterator efs = elm.getElmLineForcesList().listIterator();
			while (efs.hasNext()) {
				
				ElementForce ef1 = (ElementForce)efs.next();
				ElementForce ef2 = new ElementForce();
				PointForce pf1a = (PointForce)ef1.getPointForcesList().get(0);
				PointForce pf1b = (PointForce)ef1.getPointForcesList().get(1);
				PointForce pf2a = new PointForce();
				PointForce pf2b = new PointForce();
				
				pf2a.getCoord().x = pf1a.getCoord().x;
				pf2a.setForce(0, pf1a.getForce(0));
				pf2a.setForce(1, pf1a.getForce(1));
				pf2a.setForce(2, pf1a.getForce(2));
				pf2a.setForce(3, pf1a.getForce(3));
				pf2a.setForce(4, pf1a.getForce(4));
				pf2a.setForce(5, pf1a.getForce(5));
				
				pf2b.getCoord().x = pf1b.getCoord().x;
				pf2b.setForce(0, pf1b.getForce(0));
				pf2b.setForce(1, pf1b.getForce(1));
				pf2b.setForce(2, pf1b.getForce(2));
				pf2b.setForce(3, pf1b.getForce(3));
				pf2b.setForce(4, pf1b.getForce(4));
				pf2b.setForce(5, pf1b.getForce(5));
				
				ef2.getPointForcesList().add(0, pf2a);
				ef2.getPointForcesList().add(1, pf2b);
				
				distForces.add(ef2);
			} 
			
			if (distForces.size() > 0) {
				ElementForce dforce = (ElementForce)distForces.get(0);
				PointForce iforce = (PointForce)dforce.getPointForcesList().get(0);
				PointForce fforce = (PointForce)dforce.getPointForcesList().get(1);
				distAField = new DecimalField(iforce.getCoord().x, 7, numberFormat);
				distBField = new DecimalField(fforce.getCoord().x, 7, numberFormat);
				distFx1Field = new DecimalField(iforce.getForce(0), 7, numberFormat);
				distFy1Field = new DecimalField(iforce.getForce(1), 7, numberFormat);
				distFz1Field = new DecimalField(iforce.getForce(2), 7, numberFormat);
				distMx1Field = new DecimalField(iforce.getForce(3), 7, numberFormat);
				distFx2Field = new DecimalField(fforce.getForce(0), 7, numberFormat);
				distFy2Field = new DecimalField(fforce.getForce(1), 7, numberFormat);
				distFz2Field = new DecimalField(fforce.getForce(2), 7, numberFormat);
				distMx2Field = new DecimalField(fforce.getForce(3), 7, numberFormat);
			} else {
				distAField = new DecimalField(0, 7, numberFormat);
				distBField = new DecimalField(0, 7, numberFormat);
				distFx1Field = new DecimalField(0, 7, numberFormat);
				distFy1Field = new DecimalField(0, 7, numberFormat);
				distFz1Field = new DecimalField(0, 7, numberFormat);
				distMx1Field = new DecimalField(0, 7, numberFormat);
				distFx2Field = new DecimalField(0, 7, numberFormat);
				distFy2Field = new DecimalField(0, 7, numberFormat);
				distFz2Field = new DecimalField(0, 7, numberFormat);
				distMx2Field = new DecimalField(0, 7, numberFormat);
				distAField.setText("");
				distBField.setText("");
				distFx1Field.setText("");
				distFy1Field.setText("");
				distFz1Field.setText("");
				distMx1Field.setText("");
				distFx2Field.setText("");
				distFy2Field.setText("");
				distFz2Field.setText("");
				distMx2Field.setText("");
				distModButton.setEnabled(false);
			}
			
		} else {
			
			//Setting distributed forces fields
			distAField = new DecimalField(0, 7, numberFormat);
			distBField = new DecimalField(0, 7, numberFormat);
			distFx1Field = new DecimalField(0, 7, numberFormat);
			distFy1Field = new DecimalField(0, 7, numberFormat);
			distFz1Field = new DecimalField(0, 7, numberFormat);
			distMx1Field = new DecimalField(0, 7, numberFormat);
			distFx2Field = new DecimalField(0, 7, numberFormat);
			distFy2Field = new DecimalField(0, 7, numberFormat);
			distFz2Field = new DecimalField(0, 7, numberFormat);
			distMx2Field = new DecimalField(0, 7, numberFormat);
			distAField.setText("");
			distBField.setText("");
			distFx1Field.setText("");
			distFy1Field.setText("");
			distFz1Field.setText("");
			distMx1Field.setText("");
			distFx2Field.setText("");
			distFy2Field.setText("");
			distFz2Field.setText("");
			distMx2Field.setText("");
			distModButton.setEnabled(false);
			
		}
		
		//Enabling or disabling fields according to analysis model type
		if (this.parent.getController().getSelection().size() == 1)
			anlType = ((FrameElement)this.parent.getController().getSelection().get(0)).getAnalysisModel().getType();
		else anlType = fmodel.getGlobalAnalysisModel().getType();
		
		if (anlType.equals("Beam")) {
			
			distFx1Field.setEnabled(false);
			distFy1Field.setEnabled(true);
			distFz1Field.setEnabled(false);
			distMx1Field.setEnabled(false);
			distFx2Field.setEnabled(false);
			distFy2Field.setEnabled(true);
			distFz2Field.setEnabled(false);
			distMx2Field.setEnabled(false);
			
		} else if (anlType.equals("PlaneFrame")) {
			
			distFx1Field.setEnabled(true);
			distFy1Field.setEnabled(true);
			distFz1Field.setEnabled(false);
			distMx1Field.setEnabled(false);
			distFx2Field.setEnabled(true);
			distFy2Field.setEnabled(true);
			distFz2Field.setEnabled(false);
			distMx2Field.setEnabled(false);
			
		} else if (anlType.equals("PlaneTruss")) {
			
			distFx1Field.setEnabled(true);
			distFy1Field.setEnabled(true);
			distFz1Field.setEnabled(false);
			distMx1Field.setEnabled(false);
			distFx2Field.setEnabled(true);
			distFy2Field.setEnabled(true);
			distFz2Field.setEnabled(false);
			distMx2Field.setEnabled(false);
		
		} else if (anlType.equals("Grid")) {
			
			distFx1Field.setEnabled(false);
			distFy1Field.setEnabled(true);
			distFz1Field.setEnabled(false);
			distMx1Field.setEnabled(true);
			distFx2Field.setEnabled(false);
			distFy2Field.setEnabled(true);
			distFz2Field.setEnabled(false);
			distMx2Field.setEnabled(true);
			
		}
		
	}
	
//*****************************************************************************
	
	public void setUpDistList() {
		
		int num = distForces.size();
		
		if (num > 0) {
			distLabelList = new String[num];
			for (int i=0; i<num; i++)
				distLabelList[i] = (parent.getResourceBundle().getString("dElmDistForDialog05") + " " + (i+1));
		} else {
			distLabelList = new String[1];
			distLabelList[0] = "----- " + parent.getResourceBundle().getString("dElmDistForDialog06") + " -----";
		}
		
	}
	
//*****************************************************************************
	
	public void setUpDistCombo() {
		
		distListCombo.removeAllItems();
		for (int i=0; i<distLabelList.length; i++)
			distListCombo.addItem(distLabelList[i]);
		
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class DistComboListener implements ItemListener {
		public void itemStateChanged(ItemEvent evt) {
			
			int index = distListCombo.getSelectedIndex();
			int listSize = distForces.size();
			
			if (listSize>0 && index>=0) {
				
				ElementForce dforce = (ElementForce)distForces.get(index);
				PointForce iforce = (PointForce)dforce.getPointForcesList().get(0);
				PointForce fforce = (PointForce)dforce.getPointForcesList().get(1);
				
				distAField.setValue(iforce.getCoord().x);
				distBField.setValue(fforce.getCoord().x);
				distFx1Field.setValue(iforce.getForce(0));
				distFy1Field.setValue(iforce.getForce(1));
				distFz1Field.setValue(iforce.getForce(2));
				distMx1Field.setValue(iforce.getForce(3));
				distFx2Field.setValue(fforce.getForce(0));
				distFy2Field.setValue(fforce.getForce(1));
				distFz2Field.setValue(fforce.getForce(2));
				distMx2Field.setValue(fforce.getForce(3));
				
				distModButton.setEnabled(true);
			
			} else {
				
				distAField.setText(" ");
				distBField.setText(" ");
				distFx1Field.setText(" ");
				distFy1Field.setText(" ");
				distFz1Field.setText(" ");
				distMx1Field.setText(" ");
				distFx2Field.setText(" ");
				distFy2Field.setText(" ");
				distFz2Field.setText(" ");
				distMx2Field.setText(" ");
				
				distModButton.setEnabled(false);
				
			}
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class DistNewListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(distButPane, "distLabel");
			setButtonsEnabled(false);
			
			distAField.setEditable(true);
			distBField.setEditable(true);
			distFx1Field.setEditable(true);
			distFy1Field.setEditable(true);
			distFz1Field.setEditable(true);
			distMx1Field.setEditable(true);
			distFx2Field.setEditable(true);
			distFy2Field.setEditable(true);
			distFz2Field.setEditable(true);
			distMx2Field.setEditable(true);
			
			int index = distForces.size()+1;
			
			distAField.setValue(0);
			
			if (parent.getController().getSelection().size() == 1 ) {
				FrameElement elm = (FrameElement)parent.getController().getSelection().get(0);
				distBField.setValue(elm.getLength());
				distBField.setText(numberFormat2.format(elm.getLength()));
			} else {
				distBField.setValue(0);
			}
			
			distLabelField.setText(parent.getResourceBundle().getString("dElmDistForDialog05") + " " + index);
			
			distFx1Field.setValue(0);
			distFy1Field.setValue(0);
			distFz1Field.setValue(0);
			distMx1Field.setValue(0);
			distFx2Field.setValue(0);
			distFy2Field.setValue(0);
			distFz2Field.setValue(0);
			distMx2Field.setValue(0);
			
			distNewPressed = true;
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class DistModListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(distButPane, "distLabel");
			setButtonsEnabled(false);
			
			distNewPressed = false;
			
			int index = distListCombo.getSelectedIndex();
			
			distLabelField.setText(parent.getResourceBundle().getString("dElmDistForDialog05") + " " + (index+1));
			
			distAField.setEditable(true);
			distBField.setEditable(true);
			distFx1Field.setEditable(true);
			distFy1Field.setEditable(true);
			distFz1Field.setEditable(true);
			distMx1Field.setEditable(true);
			distFx2Field.setEditable(true);
			distFy2Field.setEditable(true);
			distFz2Field.setEditable(true);
			distMx2Field.setEditable(true);
			
			if (anlType.equals("Beam")) {
				distFx1Field.setEnabled(false);
				distFy1Field.setEnabled(true);
				distFz1Field.setEnabled(false);
				distMx1Field.setEnabled(false);
				distFx2Field.setEnabled(false);
				distFy2Field.setEnabled(true);
				distFz2Field.setEnabled(false);
				distMx2Field.setEnabled(false);
				
			} else if (anlType.equals("PlaneFrame")) {
				
				distFx1Field.setEnabled(true);
				distFy1Field.setEnabled(true);
				distFz1Field.setEnabled(false);
				distMx1Field.setEnabled(false);
				distFx2Field.setEnabled(true);
				distFy2Field.setEnabled(true);
				distFz2Field.setEnabled(false);
				distMx2Field.setEnabled(false);
				
			} else if (anlType.equals("PlaneTruss")) {
				
				distFx1Field.setEnabled(true);
				distFy1Field.setEnabled(true);
				distFz1Field.setEnabled(false);
				distMx1Field.setEnabled(false);
				distFx2Field.setEnabled(true);
				distFy2Field.setEnabled(true);
				distFz2Field.setEnabled(false);
				distMx2Field.setEnabled(false);
				
			}
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class DistOkListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			boolean dataOk = true;
			int currentIndex = distListCombo.getSelectedIndex();
			int precision = parent.getModelState().getPrecision();
			
			//Checking if the distances a and b are higher than the length of the element
			//It must be less than the length.
			if (parent.getController().getSelection().size() == 1 ) {
				FrameElement elm = (FrameElement)parent.getController().getSelection().get(0);
				double l = elm.getLength();
				double a = distAField.getValue();
				double b = distBField.getValue();
				if (a<0){
					JOptionPane.showMessageDialog(parent,
						parent.getResourceBundle().getString("dElmDistForDialog07"),
						parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
					distAField.setValue(0);
					dataOk = false;
				}else if (a>l){
					if ((a-l) > Math.pow(10, -precision)){
						JOptionPane.showMessageDialog(parent,
							parent.getResourceBundle().getString("dElmDistForDialog08"),
							parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
						dataOk = false;
					}
					distAField.setValue(elm.getLength());
				}else if (b<a){
					JOptionPane.showMessageDialog(parent,
						parent.getResourceBundle().getString("dElmDistForDialog09"),
						parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
					distBField.setValue(a);
					dataOk = false;
				}else if (b>l){
					if ((b-l) > Math.pow(10, -precision)){
						JOptionPane.showMessageDialog(parent,
							parent.getResourceBundle().getString("dElmDistForDialog10"),
							parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
						dataOk = false;
					}
					distBField.setValue(l);
				}
			}
			
			if (dataOk) {
				
				cardManager.show(distButPane, "distCombo");
				setButtonsEnabled(true);
				
				distAField.setEditable(false);
				distBField.setEditable(false);
				distFx1Field.setEditable(false);
				distFy1Field.setEditable(false);
				distFz1Field.setEditable(false);
				distMx1Field.setEditable(false);
				distFx2Field.setEditable(false);
				distFy2Field.setEditable(false);
				distFz2Field.setEditable(false);
				distMx2Field.setEditable(false);
				
				double a = distAField.getValue();
				double b = distBField.getValue();
				double fx1 = distFx1Field.getValue();
				double fy1 = distFy1Field.getValue();
				double fz1 = distFz1Field.getValue();
				double mx1 = distMx1Field.getValue();
				double fx2 = distFx2Field.getValue();
				double fy2 = distFy2Field.getValue();
				double fz2 = distFz2Field.getValue();
				double mx2 = distMx2Field.getValue();
				
				ElementForce dforce = new ElementForce();
				PointForce iforce = new PointForce();
				PointForce fforce = new PointForce();
				
				if (distNewPressed) {
					
					iforce.setForce(0, fx1);
					iforce.setForce(1, fy1);
					iforce.setForce(2, fz1);
					iforce.setForce(3, mx1);
					iforce.setCoord(a, 0, 0);
					fforce.setForce(0, fx2);
					fforce.setForce(1, fy2);
					fforce.setForce(2, fz2);
					fforce.setForce(3, mx2);
					fforce.setCoord(b, 0, 0);
					
					dforce.getPointForcesList().add(0, iforce);
					dforce.getPointForcesList().add(1, fforce);
					
					distForces.add(dforce);
					
					setUpDistList();
					setUpDistCombo();
					distListCombo.setSelectedIndex(distForces.size()-1);
					
					distNewPressed = false;
					
				} else {
					
					dforce = (ElementForce)distForces.get(currentIndex);
					iforce = (PointForce)dforce.getPointForcesList().get(0);
					fforce = (PointForce)dforce.getPointForcesList().get(1);
					iforce.setForce(0, fx1);
					iforce.setForce(1, fy1);
					iforce.setForce(2, fz1);
					iforce.setForce(3, mx1);
					iforce.setCoord(a, 0, 0);
					fforce.setForce(0, fx2);
					fforce.setForce(1, fy2);
					fforce.setForce(2, fz2);
					fforce.setForce(3, mx2);
					fforce.setCoord(b, 0, 0);
					
					distListCombo.setSelectedIndex(currentIndex);
				}
				
			}
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class DistCancListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(distButPane, "distCombo");
			setButtonsEnabled(true);
			
			distAField.setEditable(false);
			distBField.setEditable(false);
			distFx1Field.setEditable(false);
			distFy1Field.setEditable(false);
			distFz1Field.setEditable(false);
			distMx1Field.setEditable(false);
			distFx2Field.setEditable(false);
			distFy2Field.setEditable(false);
			distFz2Field.setEditable(false);
			distMx2Field.setEditable(false);
			
			int listSize = distForces.size();
			int index = distListCombo.getSelectedIndex();
			
			if (listSize==0) {
				
				distAField.setText("");
				distBField.setText("");
				distFx1Field.setText("");
				distFy1Field.setText("");
				distFz1Field.setText("");
				distMx1Field.setText("");
				distFx2Field.setText("");
				distFy2Field.setText("");
				distFz2Field.setText("");
				distMx2Field.setText("");
				
			} else {
				ElementForce dforce = (ElementForce)distForces.get(index);
				PointForce iforce = (PointForce)dforce.getPointForcesList().get(0);
				PointForce fforce = (PointForce)dforce.getPointForcesList().get(1);
				
				distAField.setValue(iforce.getCoord().x);
				distBField.setValue(fforce.getCoord().x);
				distFx1Field.setValue(iforce.getForce(0));
				distFy1Field.setValue(iforce.getForce(1));
				distFz1Field.setValue(iforce.getForce(2));
				distMx1Field.setValue(iforce.getForce(3));
				distFx2Field.setValue(fforce.getForce(0));
				distFy2Field.setValue(fforce.getForce(1));
				distFz2Field.setValue(fforce.getForce(2));
				distMx2Field.setValue(fforce.getForce(3));
				
			}
			
			distNewPressed = false;
			
		}
	}
	
//*****************************************************************************
}